#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import os

from common.common_models import LogDetail
from common.const import DBLogLevel, BackupTypeEnum
from common.parse_parafile import ParamFileUtil
from exchange import log
from exchange.commons.common import get_last_copy_info
from exchange.commons.const import ExchangeJobInfo, BodyErr, CopyType
from exchange.commons.exchange_exception import ExchangeErrCodeException
from exchange.commons.exchange_function_tool import log_start_end, out_result_with_job_info
from exchange.commons.exchange_param_parse import ExchangeParamParse


# Exchange检查备份类型基类
class ExchangeBackupCheckBackupType:
    def __init__(self, pid):
        self._pid = pid
        self._param = ParamFileUtil.parse_param_file(pid)

    @log_start_end()
    @out_result_with_job_info()
    def do_check_backup_type_task(self, job_info: ExchangeJobInfo):
        # 全备直接返回
        backup_type = ExchangeParamParse.get_type(self._param)
        if backup_type == BackupTypeEnum.FULL_BACKUP:
            return True
        if backup_type == BackupTypeEnum.LOG_BACKUP:
            last_copy_info = get_last_copy_info(job_info, self._param, CopyType.ALL_COPY)
            if last_copy_info:
                return True
            else:
                log.error("no copy log to full")
                raise ExchangeErrCodeException(log_detail=LogDetail(
                    logDetail=BodyErr.ERR_INC_TO_FULL.value,
                    logDetailParam=[""],
                    logLevel=DBLogLevel.ERROR))

        # 检查data仓下是否有数据，无则说明没做过备份，转全备
        ret = self.check_can_backup_inc(job_info)
        if not ret:
            log.error("backup_prerequisite FAILED! check_can_backup_inc!")
            raise ExchangeErrCodeException(log_detail=LogDetail(
                logDetail=BodyErr.ERR_INC_TO_FULL.value,
                logDetailParam=[""],
                logLevel=DBLogLevel.ERROR))
        return True

    def get_job_info(self):
        job_info = ExchangeJobInfo(pid=self._pid,
                                   job_id=ExchangeParamParse.get_job_id(self._param),
                                   sub_job_id=ExchangeParamParse.get_sub_job_id(self._param),
                                   copy_id=ExchangeParamParse.get_copy_id(self._param),
                                   backup_type=ExchangeParamParse.get_type(self._param),
                                   meta_path=ExchangeParamParse.get_meta_path(self._param),
                                   data_reps=ExchangeParamParse.get_data_reps(self._param),
                                   cache_path=ExchangeParamParse.get_cache_path(self._param),
                                   backup_result=ExchangeParamParse.get_backup_result(self._param))
        return job_info

    @log_start_end()
    def check_can_backup_inc(self, job_info: ExchangeJobInfo):
        data_path = ExchangeParamParse.get_data_path(self._param)
        modify_path = data_path.replace("/", "\\")
        file_list = os.listdir(modify_path)
        if len(file_list) > 0:
            return True
        else:
            return False
